#!/usr/bin/env bash

# Set script_dir
script_dir="$(dirname "$(realpath "$0")")"
cur_dir=$(pwd)

# Source util functions
source "${script_dir}/_util"

ran_col_str "Utility or creating manifest templates for Kubernetes"

command -v kubectl >/dev/null || error_msg "Please install kubectl" 1
command -v fzf >/dev/null || error_msg "Please install fzf" 1
command -v gum >/dev/null || error_msg "Please install gum CLI utility: https://github.com/charmbracelet/gum" 1

templates=(
	"clusterrole"
	"clusterrolebinding"
	"configmap"
	"cronjob"
	"deployment"
	"ingress"
	"job"
	"namespace"
	"pod"
	"poddisruptionbudget"
	"priorityclass"
	"quota"
	"secret"
	"service"
	"serviceaccount"
)

resource_name="template"

selected_resource=$(
	echo "${templates[@]}" |
		tr ' ' '\n' |
		fzf \
			--header 'Select template' \
			--preview 'kubectl explain {1}' \
			--preview-label 'Kubectl Explain' \
			--preview-window 'right:80%'
)

case $selected_resource in
clusterrole)
	echo "clusterrole"
	;;
clusterrolebinding)
	echo "clusterrolebinding"
	;;
configmap)
	echo "configmap"
	;;
cronjob)
	# Set input variables
	namespace=$(gum input --value "default" --header="Enter namespace for resource")
	success_msg "Namespace '${namespace}'"
	resource_name=$(gum input --value "${resource_name}" --header="Enter resource name")
	success_msg "Resource name '${resource_name}'"
	image_name=$(gum input --value "alpine:latest" --header="Set Image name")
	success_msg "Image '${image_name}'"
	cron_schedule=$(gum input --value "15 * * * *" --header="Set Cron schedule")
	success_msg "Cron schedule '${cron_schedule}'"
	restart_policy=$(gum choose --header="Set restart policy" "OnFailure" "Never")
	success_msg "Restart policy '${restart_policy}'"
	cmd=$(gum write --char-limit=0 --show-line-numbers --value "/bin/sh -c -- date" --header="Command to run")
	success_msg "Command: ${cmd}"

	# Create resource
	file_name="${resource_name}.${selected_resource}.yaml"
	kubectl create cronjob "${resource_name}" \
		--namespace="${namespace}" \
		--image="${image_name}" \
		--schedule="${cron_schedule}" \
		--restart="${restart_policy}" \
		--dry-run=client \
		--output=yaml \
		-- ${cmd} \
		>$file_name
	;;
deployment)
	# Set input variables
	namespace=$(gum input --value "default" --header="Enter namespace for resource")
	success_msg "Namespace '${namespace}'"
	resource_name=$(gum input --value "${resource_name}" --header="Enter resource name")
	success_msg "Resource name '${resource_name}'"
	image_name=$(gum input --value "alpine:latest" --header="Set Image name")
	success_msg "Image '${image_name}'"
	container_port=$(gum input --value="-1" --header="Container expose port (-1 = none)")
	success_msg "Port ${container_port}"
	replicas=$(gum input --value "1" --header="Number of replicas")
	success_msg "Replicas ${replicas}"
	cmd=$(gum write --char-limit=0 --show-line-numbers --value "/bin/sh -c -- echo hello" --header="Command to run")
	success_msg "Command: ${cmd}"

	# Create resource
	file_name="${resource_name}.${selected_resource}.yaml"
	kubectl create deployment "${resource_name}" \
		--namespace="${namespace}" \
		--image="${image_name}" \
		--port=${container_port} \
		--replicas=${replicas} \
		--dry-run=client \
		--output=yaml \
		-- ${cmd} \
		>$file_name
	;;
ingress)
	echo "ingress"
	;;
job)
	echo "job"
	;;
namespace)
	# Set input variables
	namespace=$(gum input --value "default" --header="Enter namespace for resource")
	success_msg "Namespace '${namespace}'"
	resource_name=$(gum input --value "${resource_name}" --header="Enter resource name")
	success_msg "Resource name '${resource_name}'"

	# Create resource
	file_name="${resource_name}.${selected_resource}.yaml"
	kubectl create namespace "${namespace}" \
		--dry-run=client \
		--output=yaml \
		-- ${cmd} \
		>$file_name
	;;
pod)
	echo "pod"
	;;
poddisruptionbudget)
	echo "poddisruptionbudget"
	;;
priorityclass)
	echo "priorityclass"
	;;
quota)
	echo "quota"
	;;
secret)
	echo "secret"
	;;
service)
	echo "service"
	;;
serviceaccount)
	echo "serviceaccount"
	;;
*) error_msg "Unknown template $selected_resource" 1 ;;
esac

if [[ "$?" -eq 0 ]]; then
	success_msg "Template successfully saved as ${file_name}"
	sleep 1
	gum pager --show-line-numbers --soft-wrap <${file_name}
else
	error_msg "Error saving ${file_name}" 1
fi
